/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.geoviewer.jxmap;

import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;

/**
 *
 * @author Timon Veenstra
 */
public class ColourManipulator {

    //        Image img = TransformGrayToTransparency(createImage(layerPanel, BufferedImage.TYPE_INT_ARGB));
//        this.layeredPaneDragImage = ImageToBufferedImage(img, layerPanel.getSize().width, layerPanel.getSize().height);

    private Image TransformGrayToTransparency(BufferedImage image) {
        ImageFilter filter = new TransparentImageFilter() ;
        ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
        return Toolkit.getDefaultToolkit().createImage(ip);
    }

    class TransparentImageFilter extends RGBImageFilter {

        float alphaPercent;

        public TransparentImageFilter() {
            this(0.75f);
        }

        public TransparentImageFilter(float aPercent)
                throws IllegalArgumentException {
            if ((aPercent < 0.0) || (aPercent > 1.0)) {
                throw new IllegalArgumentException();
            }
            alphaPercent = aPercent;
            canFilterIndexColorModel = true;
        }

        public int filterRGB(int x, int y, int rgb) {
            int bgColor = Color.yellow.getRGB();
            if (rgb == bgColor){
               return 0x00000000;
            }
            return rgb;

//            int a = (rgb >> 24) & 0xff;
//            a *= alphaPercent;
//            return ((rgb & 0x00ffffff) | (a << 24));
        }
    }
}
